package com.hserver.docker.controller;

import cn.hserver.plugin.web.annotation.Controller;
import cn.hserver.plugin.web.annotation.GET;
import cn.hserver.plugin.web.annotation.POST;
import cn.hserver.plugin.web.context.WebConstConfig;
import cn.hserver.plugin.web.interfaces.HttpRequest;
import cn.hserver.plugin.web.interfaces.HttpResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hserver.docker.bean.Deploy;
import com.hserver.docker.config.ConstConfig;
import com.hserver.docker.service.DeployService;
import com.hserver.docker.service.DockerService;
import com.hserver.docker.service.RecordService;
import cn.hserver.core.ioc.annotation.*;
import cn.hserver.core.server.util.JsonResult;


import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller("/project/")
public class ProjectController {
    @Autowired
    private DeployService deployService;

    @Autowired
    private DockerService dockerService;

    @Autowired
    private RecordService recordService;

    /**
     * 项目列表页面
     *
     * @param httpResponse
     */
    @GET("projectIndex")
    public void projectIndex(HttpResponse httpResponse) {
        Map<String, Object> data = new HashMap<>();
        data.put("dockerUrl", ConstConfig.DOCKER_URL);
        httpResponse.sendTemplate("/project/projectList.ftl", data);
    }

    /**
     * 查看所有项目
     *
     * @return
     */
    @GET("getProjectList")
    public JsonResult getProjectList(HttpRequest request) {
        String rawData = request.getRawData();
        ObjectMapper json = WebConstConfig.JSON;
        if (!rawData.contains("name")) {
            return JsonResult.ok().put("data", deployService.getList());
        }
        String name = null;
        try {
            Map readValue = json.readValue(rawData, Map.class);
            name = readValue.get("name").toString();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (!name.equals("")) {
            return JsonResult.ok().put("data", deployService.getProjectList(name));
        }
        return JsonResult.ok().put("data", deployService.getList());
    }

    /**
     * 新增项目
     *
     * @param deploy
     * @param response
     */
    @POST("addProject")
    public void addProject(Deploy deploy, HttpResponse response) {
        String uuid = "";
        if (deploy.getUuid() != null && deploy.getUuid().trim().length() > 0) {
            uuid = deploy.getUuid();
            deployService.update(deploy);
        } else {
            uuid = UUID.randomUUID().toString();
            deploy.setUuid(uuid);
            deployService.insert(deploy);
        }
        response.redirect("/project/projectDesc/" + uuid);
    }

    /**
     * 项目详情页
     *
     * @param request
     * @param response
     */
    @GET("projectDesc/{uuid}")
    public void projectDesc(HttpRequest request, HttpResponse response) {
        Map<String, Object> data = new HashMap<>(1);
        data.put("detail", deployService.getDeploy(request.query("uuid")));
        data.put("recordList", recordService.getList(request.query("uuid")));
        response.sendTemplate("project/projectDesc.ftl", data);
    }


    /**
     * 项目编辑也和项目添加页
     *
     * @param request
     * @param response
     */
    @GET("projectAddOrEdit/{uuid}")
    public void projectAddOrEdit(HttpRequest request, HttpResponse response) {
        Map<String, Object> data = new HashMap<>(1);
        if (request.query("uuid") != null && !request.query("uuid").equals("0")) {
            data.put("detail", deployService.getDeploy(request.query("uuid")));
            response.sendTemplate("project/projectAddOrEdit.ftl", data);
        } else {
            data.put("detail", new Deploy());
            response.sendTemplate("project/projectAddOrEdit.ftl");
        }
    }


    @GET(value = "del/{uuid}")
    public JsonResult del(HttpRequest request) {
        deployService.del(request.query("uuid"));
        return JsonResult.ok();
    }

}
